<?php
/*
 * 
 * Founded on 17.5.2006 by Michal Hantl
 *
 */
 
if (version_compare(phpversion(), "5.0.0","<")) die("<h1>This CMS runs on PHP 5.0.0 and higher only!</h1>"); 
 
class cCms {
		
	public $nstree;
	public $siteDir;
	
	/**
	 * Sem muzou objekty hazet onFly cache
	 *
	 * @cache array
	 */
	public $onFlyCache;
		
	function __construct($siteDir, $fromDir, $dbUser, $dbName, $dbPass="", $dbHost="localhost")
	{
		//start stopwatch
		$this->startTime = $this->getMicrotime();
		
		$dir = $this->coreDir = dirname(__FILE__)."/";
		$this->siteDir = $siteDir;
		
		$this->siteUrl = 'http://'.$_SERVER["HTTP_HOST"].substr($_SERVER['REQUEST_URI'],0,-(strlen($s = strrchr(substr($_SERVER['REQUEST_URI'],0,-1),'/'))));
		
		$this->fromDir = $fromDir;
		
		if (!@$this->link=mysql_connect($dbHost, $dbUser, $dbPass)) die("<h1>Nepodařilo se připojit k databázi:(</h1>");
		if (!@mysql_select_db($dbName)) die("<h1>Nepodařilo se vyselektovat databázi:(</h1>");
		mysql_query("SET CHARACTER SET utf8");

		include($dir."debugger.php");
		$this->debugger = new cDebugger($this);		

		include($dir."exception.php");

		include($dir."tree.php");
		$this->tree =  new cTree($this);

		include($dir."view.php");
		$this->defaultView = new cView(array("lang"=>"cs","length"=>"full","id"=>1,"action"=>"show","doctype"=>"xhtml","mode"=>"frontend","page"=>1), null,  $this);

		include($dir."group.php");

		include($dir."file.php");
		$this->file = new cFile($this);		

		include($dir."cache.php");
		$this->cache = new cCache($this);
		
		include($dir."part.php");
		include($dir."node.php");
		include($dir."event.php");
		$this->file->loadNode("standard","treeItem");

		$this->file->loadCore('nstree');
		$this->nstree = new cNSTree($this);
		
		$this->vars = $this->getVars();
		
		$userClass = $this->file->loadNode("standard","user");

		$this->user = new $userClass($this);

		$this->user->recognize();

	}
	
	function runWebPages() 
	{
		
		$this->defaultView = new cView(array("mode"=>"frontend","id"=>1,"action"=>"show"), $this->defaultView, $this);
		$this->view = new cView(array(),$this->defaultView);
		$this->view->recognize();
		
		if ($r = $this->cache->cachedResult($this->view)) 
		{
			return $r;
		}
		
		if (($url = $this->view->getUrl())=="./") $url = "";
		$this->url = $url;

		$layout = $this->tree->getNode("parent_id=1 AND type='layout'");
		//print_r($layout);
		$this->layout = $layout;
		$layout->refresh();
		
		$output = $layout->view($this->view);
		
		// cache v databazi
		$this->cache->cachePage($output, $this->view);
		
		//pokud nejsme v debug modu
		if (!$this->debugger->debug) {
			return $output;
		}
	}
	
	function getVars() 
	{
		return array(
						"name"=>"Oxpac",
						"version"=>"0.7.5",
						"autor"=>"Michal Hantl",
						"homepage"=>"http://hantl.info/cms",
						//""=>"",
					);
	}

	function getVar($name) 
	{
		return $this->vars[$name];
	}
	
	function getExecTime() 
	{
		return $this->getMicroTime() - $this->startTime;	
	}

	function getMicrotime() 
	{
	  list($usec, $sec) = explode(" ",microtime());
	  return ((float)$usec + (float)$sec);
	}
		
} 

?>